//
//  THCPCCompleteDetailVC.m
//  YJB
//
//  Created by 中电大维 on 2017/1/5.
//  Copyright © 2017年 YJB. All rights reserved.
//

#import "THCPCCompleteDetailVC.h"
#import "THCPCMyPublishDetailCell.h"
#import "THCPCMyPublishCompleteDVC.h"
#import "THCPCMyPublishDetailModel.h"

@interface THCPCCompleteDetailVC ()

@property(nonatomic,strong)NSMutableArray *tasks;

@end

@implementation THCPCCompleteDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"参与完成详情";
    self.tableView.rowHeight = 50;
    self.tableView.backgroundColor = kUIColorFromRGB(0xeeeeee);
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[THCPCMyPublishDetailCell class] forCellReuseIdentifier:completeDetailID];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)loadData
{
    /*
     获取{"action":"get_myinvolve_list","makegold":{"username":"用户名","firstrow":"当前页码（-1：获取全部；>=0：页码）","listrows":"每页数目（不传默认为10）","user_info":"用户信息","phone_info":"手机信息","md5_key":"密钥"}}
     */
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *userName = [YJUserDefaultTool readUserName];
    NSString *md5KeyId = [YJUserDefaultTool readKeyID];
    params[@"action"] = @"get_myinvolve_list";
    params[@"makegold"] = @{@"username":userName,
                            @"firstrow":@"-1",
                            @"listrows":@"",
                            @"user_info":userName,
                            @"phone_info":@"iOS",
                            @"md5_key":md5KeyId};
    [YJNetworkTool postWithUrl:THURL(@"cpcclick.php") parameters:params success:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        NSString *code = responseObject[@"return"][@"code"];
        if (code.integerValue == 200)
        {
            NSArray *array = responseObject[@"return"][@"data"][@"list"];
             _tasks = [THCPCMyPublishDetailModel mj_objectArrayWithKeyValuesArray:array];
        }
        else if(code.integerValue == 500)
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络不佳，稍后重试" toView:self.view];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //    return self.tasks.count;
    return 5;
}

#pragma mark ----------------------- 表格代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return 1;
    return _tasks.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    THCPCMyPublishDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:completeDetailID forIndexPath:indexPath];
    cell.tag = indexPath.section;
    cell.model = _tasks[indexPath.section];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    THCPCMyPublishCompleteDVC *vc = [[THCPCMyPublishCompleteDVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (0 == section) {
        return 10;
    }
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 52;
}

- (NSMutableArray *)tasks
{
    if (!_tasks) {
        _tasks = [[NSMutableArray alloc] init];
    }
    return _tasks;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
